package com.qqtech.qquan.order.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.order.enums.OrderConst;
import com.qqtech.qquan.order.model.OrderPayLog;
import com.qqtech.qquan.order.model.OrderPayLogQuery;
import com.qqtech.qquan.order.model.OrderPayLogVo;
import com.qqtech.qquan.order.service.OrderPayLogService;
import com.qqtech.qquan.order.service.OrderSysDictService;
import com.qqtech.qquan.sys.controller.BaseAdminController;
import com.qqtech.qquan.sys.enums.SysConst;
import com.qqtech.qquan.sys.model.SysParam;
import com.qqtech.qquan.sys.service.SysParamService;
import com.qqtech.qquan.util.ExcelUtil;

@Controller
@RequestMapping(value = "/order/pay/log")
public class OrderPayLogController extends BaseAdminController{
	@Resource
	private OrderPayLogService orderPayLogService;
	@Resource
	private SysParamService sysParamService;
	@Resource
	private OrderSysDictService  orderSysDictService;
    
	@Override
	protected BaseService<OrderPayLog> getBaseService() {
		return orderPayLogService;
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "/order/payLog/payLog-list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(OrderPayLogQuery query, HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(), new PageOrder(Direction.DESC, "id"));
		Page<OrderPayLogVo> page = orderPayLogService.queryPageListAdmin(query, pageKit);
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}
	/**
	 * @param query
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/export2Excel")
	@ResponseBody
	public Result export2Excel(OrderPayLogQuery query, HttpServletRequest request, HttpServletResponse response) {
		Result ret = new Result();
		Pageable pageKit = new PageKit(1, Integer.MAX_VALUE, new PageOrder(Direction.DESC, "newAt"));
		int count = orderPayLogService.queryCount(query);
		SysParam sysParam = sysParamService.getByKey(SysConst.PARAM_SYS_EXPORT_TOEXCEL_MAXROWS);
		if (sysParam != null && StringUtil.isNotBlank(sysParam.getValue())) {
			int maxExportRows = Integer.valueOf(sysParam.getValue());
			if (count > maxExportRows) {
				ret.addErr("导出数量不能超过" + maxExportRows + "行!");
				return ret;
			}
		}
		List<OrderPayLogVo> moneyLogList = orderPayLogService.queryListAdmin(query, pageKit);// 不分页
		List<Map<String, Object>> list = createExcelRecord(moneyLogList);
		String columnNames[] = { "昵称", "对应系统的订单号", "支付交易号", "支付人(email)账号或者微信的openid", "支付额度", "支付方式", "时间"};// 列名
		String keys[] = { "userNick", "orderNo", "tradeNo", "buyerEmail", "value", "payWay", "strNewAt"};// map中的key
		String fileName = "支付流水";
		ExcelUtil.ExcelSingleOutputStream(list, keys, columnNames, fileName, request, response);
		return ret;
	}

	private List<Map<String, Object>> createExcelRecord(List<OrderPayLogVo> moneyLogList) {
		List<Map<String, Object>> listmap = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sheetName", "支付流水");
		listmap.add(map);
		OrderPayLogVo logVo = null;
		
		for (int j = 0; j < moneyLogList.size(); j++) {
			logVo = moneyLogList.get(j);
			Map<String, Object> mapValue = new HashMap<String, Object>();
			mapValue.put("userNick", logVo.getUserNick());
			mapValue.put("orderNo", logVo.getOrderNo());
			mapValue.put("tradeNo", logVo.getTradeNo());
			mapValue.put("buyerEmail", logVo.getBuyerEmail());
			mapValue.put("value", logVo.getValue());
			mapValue.put("payWay", orderSysDictService.dictValue(OrderConst.DICTYPE_ORDER_PAYWAY, String.valueOf(logVo.getPayWay())));
			mapValue.put("strNewAt", logVo.getStrNewAt());
			listmap.add(mapValue);
		}
		return listmap;
	}
}
